<?php
//买家资产账户类
class App_Account extends Data_Base
{
	//获取账户信息
	//$user 用户对象
    public static function account ($user)
    {
        $uid = $user->getUserUid();
        $ret   = array('coin' => 0, 'tickets' => array(), 'countsOrder' => 0, 'moneyOrder' => 0);

        if ($uid == 0)
            return $ret;

        self::initAccount($uid);

        $query = DB::select()
            ->from('user_v2_account')
            ->where('uid', '=', $uid)
            ->execute();

        $ret = $query->current();
        $ret['tickets'] = self::accountTickets($uid);

        // 晒单数量
        $ret['countsShai'] = self::counts('app_order_shai', array('uid', '=', $uid));

        $follow = new App_Follow($uid);
        $follow->setGroupType('user');

        // 关注数
        $ret['countsFollow'] = $follow->countsFollow();

        // 粉丝数
        $ret['countsFans']   = $follow->countsFollowed();

        return $ret;
    }

    // 账户金币余额
    public static function coin ($uid)
    {
        $query = DB::select()
            ->from('user_v2_account')
            ->where('uid', '=', $uid)
            ->execute();
        $info = $query->current();
        return Arr::get($info, 'coin', 0);
    }


	//优惠券
	//$ticketId int 优惠券id
    public static function ticketMoney ($ticketId)
    {
        return 0;
    }

	//账户信息列表
    public static function infoList ($offset, $limit, $includeUserInfo = FALSE)
    {
        if ($includeUserInfo)
        {
            $query = DB::select('a.coin', 'u.*')
                ->from(array('user_v2', 'u'))
                ->join(array('user_v2_account', 'a'), 'LEFT')
                ->on('u.uid', '=', 'a.uid')
                ->offset($offset)
                ->limit($limit)
                ->order_by('a.coin', 'desc')
                ->execute();

            return $query->as_array();
        }
    }

	//账户金币数
    public static function accountCoin ($uid)
    {
        $query = DB::select()
            ->from('user_v2_account')
            ->where('uid', '=', $uid)
            ->limit(1)
            ->execute()
            ->current();

        return Arr::get($query, 'coin', 0);
    }

	//账户余额(分)
    public static function accountMoney ($uid)
    {
        $query = DB::select()
            ->from('user_v2_account')
            ->where('uid', '=', $uid)
            ->limit(1)
            ->execute()
            ->current();

        return Arr::get($query, 'money', 0);
    }


	//账户初始化
    public static function initAccount($uid)
    {
        if (self::counts('user_v2_account', array('uid', '=', $uid)) > 0)
            return;
        DB::insert('user_v2_account')
            ->columns(array('uid', 'coin', 'countsOrder', 'moneyOrder', 'money'))
            ->values (array($uid, 0, 0, 0, 0))
            ->execute();
    }

	//账户订单数
    public static function incAccountOrder ($uid, $number = 1)
    {
        if ($number == 0)
            return;

        DB::update('user_v2_account')
            ->set(array('countsOrder' => DB::expr("`countsOrder` + $number")))
            ->where('uid', '=', $uid)
            ->execute();
 
    }

	//设置账户消息
    public static function setNewChatMessage($uid, $message)
    {
        $msgTime = empty($message) ? 0 : DB::expr('unix_timestamp(now())');
        $row = array(
            'messageChat' => $message,
            'messageTime' => $msgTime,
        );

        DB::update('user_v2_account')
            ->set($row)
            ->where('uid', '=', $uid)
            ->execute();
    }

	//新的咨询消息
    public static function newChatMessages ($offset, $limit)
    {
        $query = DB::select('u.uid', 'u.userName', 'u.userIcon', 'a.messageChat', 'a.messageTime')
            ->from(array('user_v2_account', 'a'))
            ->join(array('user_v2', 'u'), 'LEFT')
            ->on('u.uid', '=', 'a.uid')
            ->where('a.messageTime', '>', 0)
            ->order_by('a.messageTime', 'desc')
            ->execute();

        return $query->as_array();

    }

	// 增加/消耗账户金币
    public static function addAccountCoin ($uid, $coin, $message, $coinUsed = 0)
    {
        if ($coin == 0)
            return TRUE;

        // 判断消耗账户金币是否够用
        if ($coin < 0)
        {
            $accountCoin = self::accountCoin($uid);
            $absCoin = abs($coin);
            if ($accountCoin < $absCoin)
                return FALSE;
        }

        $row = array(
            'uid' => $uid,
            'countsCoin' => $coin,
            'message'    => $message,
            'created'    => DB::expr('unix_timestamp(now())'),
        );

        list($insert_id, $rows) = DB::insert('user_v2_account_coin')
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        
        $coinAbs = abs($coin);
        if ($coin > 0)
            $row = array('coin' => DB::expr("`coin` + $coinAbs"));
        else 
            $row = array(
                'coinUsed' => DB::expr("`coinUsed` + $coinAbs"),
                'coin'     => DB::expr("`coin` - $coinAbs"),
                );

        if ($coinUsed > 0)
        {
            $row['coinUsed'] = DB::expr("`coinUsed` + $coinUsed");
        }
        else if ($coinUsed < 0)
        {
            $absCoinUsed = abs($coinUsed);
            $row['coinUsed'] = DB::expr("`coinUsed` - $absCoinUsed");
        }

        DB::update('user_v2_account')
            ->set($row)
            ->where('uid', '=', $uid)
            ->execute();
    }

	//账户优惠券
    public static function accountTickets($uid)
    {
        $query = DB::select()
            ->from('user_v2_account_ticket')
            ->where('uid', '=', $uid)
            ->where('status', '=', 1)
            ->order_by('created', 'asc')
            ->execute()
            ->as_array();
        return $query;
    }

    // 金币兑换记录
    public static function money2coinRecords ($uid)
    {
        $query = DB::select()
            ->from('user_v2_account_coin')
            ->where('uid', '=', $uid)
            ->where('countsCoin', '>', 0)
            ->order_by('created', 'desc')
            ->execute();

        return $query->as_array();
    }

    // 金币消耗记录
    public static function coinUsedRecords ($uid)
    {
        $query = DB::select()
            ->from('user_v2_account_coin')
            ->where('uid', '=', $uid)
            ->where('countsCoin', '<', 0)
            ->order_by('created', 'desc')
            ->execute();

        return $query->as_array();
    }

    public static function userOrderCount($uid){
        $query = DB::select()
            ->from('app_order')
            ->where('uid','=',$uid);

        $data['countsOrder'] = $query->where('status','=','99')->execute()->count();
        $orders = $query->where('status','=','99')->execute()->as_array();
        $data['moneyOrder'] = 0;
        foreach($orders as $v){
            if($v['moneyTotalRMB']){
                $data['moneyOrder'] += $v['moneyTotalRMB'];
            }
        }
        return $data;
//        var_dump($orders);
//        $data['orders_count'] = $query->where('','','');
//        var_dump($data);
    }

}
